package com.zhangpf.boot.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class HttpContextUtil {

	private HttpContextUtil(){

	}
	
	/**
	 * 获取当前的HttpServletRequest
	 */
	public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
	}

	/**
	 *移除入参里的 HttpServletRequest、HttpServletResponse参数
	 */
	public static Object[] removeHttpServletParams(Object[] reqParams) {
		List<Object> obs = new ArrayList<Object>(reqParams.length);
		for (Object object : reqParams) {
			if (object instanceof HttpServletRequest || object instanceof HttpServletResponse) {
				continue;
			}
			obs.add(object);
		}
		return obs.toArray();
	}
}
